package com.example.protobufdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.protobufdemo.MyADInfo.MyAdInf;
import com.example.protobufdemo.MyADInfo.MyAdInfoBeanSingle;

import proto.ADTest;
import proto.ADTest.ADInfo;
import proto.ADTest.LoginInfoRes;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class AnotherActivity extends Activity{
	
	LoginInfoRes loginInfoRes = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		addTestPro();
		
		
		//请求数据回包
		int adNum = loginInfoRes.getAdNum();
		for (int i = 0; i < adNum; i++) {
			
			ADInfo adInfo = loginInfoRes.getAdInfoList(i);
			
			int adIndex = adInfo.getAdIndex();
			String adPicUrl = adInfo.getAdPicUrl();
			String adApkUrl = adInfo.getAdApkUrl();
			
			Log.d("AAAAA", "adPicUrl:"+adPicUrl+",adApkUrl:"+adApkUrl);
			
			MyADInfo.getAdInfoInstance().addADInfo(adIndex, adPicUrl, adApkUrl);
			
		}
		
		//取数据
		MyADInfo.getAdInfoInstance().getADInfo(new MyAdInf() {
			
			@Override
			public void receiverAdInf(Object object) {
				// TODO Auto-generated method stub
				if (object instanceof MyAdInfoBeanSingle) {
					
					MyAdInfoBeanSingle adSingle = (MyAdInfoBeanSingle) object;
					
					int adIndex = adSingle.getAdIndex();
					String adPicUrl = adSingle.getAdPicUrl();
					String adApkUrl = adSingle.getAdApkUrl();
					
					
					Log.d("receiverAdInf", "adIndex:"+adIndex+",adPicUrl:"+adPicUrl+",adApkUrl:"+adApkUrl);
				
					
				}
				
				
				
				
			}
		});
		
		
		
		
		
		
		
		
		
	}
	
	//测试数据
	private void addTestPro(){
	
		ADTest.LoginInfoRes.Builder builder = ADTest.LoginInfoRes.newBuilder();
		builder.setAdNum(5);
		
		List<ADInfo> adInfoList = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			ADInfo.Builder adBuilder = ADInfo.newBuilder();
			adBuilder.setAdIndex(i+1);
			adBuilder.setAdPicUrl("picUrl:"+i);
			adBuilder.setAdApkUrl("apkUrl:"+i);
			
			ADInfo adinfo = adBuilder.build();
			adInfoList.add(i, adinfo);
		}
		builder.addAllAdInfoList(adInfoList);
		
		loginInfoRes = builder.build();
		
	}
	
	
}
